using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Authorities.Organizations;
using Wips.AppService.Authorities.Organizations.Requests;
using Wips.AppService.Authorities.Organizations.Responses;
using Wips.AppService.CommonViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 组织架构
    /// </summary>
    public class OrganizationController : CustomeBaseController
    {
        private readonly IOrganizationService _service;
        private readonly IMediator _mediator;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mediator"></param>
        public OrganizationController(
            IOrganizationService service,
            IMediator mediator)
        {
            _service = service;
            _mediator = mediator;
        }

        #region 基础接口

        /// <summary>
        /// 读取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetOrganizationPagesResponse>> GetPagesAsync(
            [FromBody] GetOrganizationPagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }


        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetOrganizationPagesResponse>> GetListAsync(
            GetOrganizationPagesRequest request)
        {
            return await _service.GetListAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetOrganizationByIdResponse> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }


        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreateOrganizationRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdateOrganizationRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeleteOrganizationRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> StatusChangeAsync([FromBody] OrganizationStatusChangeRequest request)
        {
            return await _mediator.Send(request);
        }

        #endregion

        #region 扩展接口

        /// <summary>
        /// 读取树形结构的组织架构数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List< TreeSelectViewModel>> GetTreeSelectDataAsync()
        {
            return await _service.GetTreeSelectDataAsync();
        }

        /// <summary>
        /// 读取树形列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<List<GetOrganizationTreeDataResponse>> GetTreeDataAsync(
            [FromBody] GetOrganizationTreeDataRequest request)
        {
            return await _service.GetTreeDataAsync(request);
        }

        /// <summary>
        /// 获取组织架构级联人员信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<CascaderSelectViewModel>> GetCascaderSelectDataAsync()
        {
            return await _service.GetCascaderSelectDataAsync();
        }


        /// <summary>
        /// 获取下属的组织架构人员树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<CascaderSelectViewModel>> GetSubordinateSelectDataAsync()
        {
            return await _service.GetSubordinateSelectDataAsync();
        }

        #endregion

       
    }
}